var bmap=require("../../utils/bmap-wx.js")
let app=getApp()
Page({
  data: {
    // latitude: 23.099994,
    // longitude: -113.324520,
    markers: [
      // {
      // id: 1,
      // latitude: 23.099994,
      // longitude: 113.324520,
      // name: 'T.I.T 创意园'
      // }
    ],
    // covers: [{
    //   latitude: 23.099994,
    //   longitude: 113.344520,
    //   iconPath: './location.png'
    // }, {
    //   latitude: 23.099994,
    //   longitude: 113.304520,
    //   iconPath: './location.png'
    // }]
    positionHeight:"",
    CustomBar:"",
    list:[
      
    ],
    viewInput:""
  },
  onShow(){
    wx.showLoading({
      title: '拼命加载中。。。',
      duration:2000
    })
    this.mapCtx = wx.createMapContext('myMap')
    
    setTimeout(()=>{
      this.mapCtx.moveToLocation()

      this.searchSide()
    },1500)
    
  },
  onReady(e){
    wx.getSystemInfo({
      success: e => {
        // 获取小程序页面高度
        console.log("res",e.screenHeight)
        this.setData({
          CustomBar:app.globalData.CustomBar,
          positionHeight:e.screenHeight
        })
      }
    })
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker() {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  },
  //检索周边
  searchSide(){
    var BMap = new bmap.BMapWX({
      ak: "GDr1NoCkVUMTCQxpT8ZZOXWqH16hqjMo",
    });
   
    BMap.search({
      // query: "邦华环球广场",
      fail:err=>{
        console.log("err",err)
      },
      success: res=>{
        console.log("yes",res)  
        let results = res.originalData.results
        this.setData({
          list:results
        })
        results.forEach((item,index)=>{
          item.latitude=item.location.lat
          item.longitude =item.location.lng
          item.id=index
        })
        this.setData({
          markers:results
        })
      },
    });
    
  },
  bindInput(e){
   
    this.setData({
      viewInput:e.detail.value
    })
  },
  // 自定义搜索
  toFind(){
     console.log(this.data.viewInput)
     wx.showLoading({
       title: '拼命加载中。。。',
       duration:2000
     })
     var BMap = new bmap.BMapWX({
      ak: "GDr1NoCkVUMTCQxpT8ZZOXWqH16hqjMo",
    });
   
    BMap.suggestion({
      query: this.data.viewInput,
      fail:err=>{
        console.log("err",err)
      },
      success: res=>{
        console.log("yes",res)  
        this.setData({
          list:res.result
        })
        
        res.result.forEach((item,index)=>{
           item.id=index
           item.latitude=item.location.lat,
           item.longitude=item.location.lng,
           item.name=item.address
        })
        

        this.setData({
          markers:res.result
        })
        
        this.mapCtx.moveToLocation({
          longitude:res.result[0].longitude,
          latitude:res.result[0].latitude
        })
      },
    });
    
  },
  backBusiness(e){
    app.globalData.selectedPosition={
      address:e.currentTarget.dataset.item.address,
      latitude:e.currentTarget.dataset.item.location.lat,
      longitude:e.currentTarget.dataset.item.location.lng,
      name:e.currentTarget.dataset.item.name
    }
    
    console.log(app.globalData.selectedPosition)
    wx.navigateBack()
  }
})

